** STATA GRAPHICS EXAMPLE
** LINE GRAPH 1
** v1.0.0
** IanHambleton (16-MAR-2021)

** CLOSE ANY OPEN LOG FILE AND OPEN A NEW LOG FILE (with same name as do file)
capture log close
log using graph_01_line_chart, replace
 
** DO-FILE SET UP COMMANDS
version 16
clear all
macro drop _all
set more 1
set linesize 200

** LOAD DATSET
** The dataset includes a series of health disparity measures for FOUR world regions: Caribbean (code=15), Central America (code=16), South America (code=17), Northern america (code=18)
** The THREE health disparity measures are:
**  Mean Log Deviation (measure = 4)
**  Theil Index (measure = 5)
**  Symmetrized Theil index (STI) (measure = 7)
** Calculation of indices not included in algorithm
**
** Load dataset - dataset and DO must be in same folder location
use line-graph-example-1, clear


** ------------------------------------------
** GRAPHIC FOR STI
** ------------------------------------------

** 1 - Basic graph without formatting - Caribbean only
gr twoway (connect value year if measure==7 & type==15), name(graph1)


** 2 - Add line for Central America to same graph
gr twoway (connect value year if measure==7 & type==15) (connect value year if measure==7 & type==16), name(graph2)


** 3 - Add lines for Central & South America onto same graph. Add commenting and allow command to flow over several lines of text editor (for ease of reading)
#delimit ;   /* Semi-colon needed to end a command instead of a carriage return */
	gr twoway 
          /// Central America
		  (connect value year if measure==7 & type==16)
          /// Southern America
		  (connect value year if measure==7 & type==17)
          /// Caribbean
		  (connect value year if measure==7 & type==15),
	 	  name(graph3)
		  ;   /* Notice the semi-column to end the command now */
#delimit cr


** 04 - Format X and Y AXES 
#delimit ;
	gr twoway 
		  (connect value year if measure==7 & type==16)
		  (connect value year if measure==7 & type==17)
		  (connect value year if measure==7 & type==15)
		  ,
			/// X-AXIS FORMATTING
			/// Add readable x-axis text, set text size as medium, set text 45 degree angle, set grid off
			xlab(1965 "1965-70" 1975 "1975-80" 1985 "1985-90" 1995 "1995-00" 2005 "2005-10", labs(medium) nogrid angle(45))
			/// Add x-axis title 
			xtitle("Year", margin(t=3) size(medlarge)) 
			/// Add ticks between labels
			xmtick(1965(5)2010)

			/// Y-AXIS FORMATTING
			ylab(0(20)80, labs(medium) nogrid glc(gs14) angle(0) format(%9.0f))
			ytitle("STI disparity measure", margin(r=3) size(medlarge))
			/// Add major and minor ticks between labels
			ytick(0(10)80) ymtick(0(5)80)
			name(graph4)
			;
#delimit cr


** 05 - Format LEGEND
#delimit ;
	gr twoway 
		  (connect value year if measure==7 & type==16)
		  (connect value year if measure==7 & type==17)
		  (connect value year if measure==7 & type==15)
		  ,
			xlab(1965 "1965-70" 1975 "1975-80" 1985 "1985-90" 1995 "1995-00" 2005 "2005-10", labs(medium) nogrid angle(45))
			xtitle("Year", margin(t=3) size(medlarge)) 
			xmtick(1965(5)2010)

			ylab(0(20)80, labs(medium) nogrid glc(gs14) angle(0) format(%9.0f))
			ytitle("STI disparity measure", margin(r=3) size(medlarge))
			ytick(0(10)80) ymtick(0(5)80)

			/// FORMAT LEGEND
			// Legend position is 12 o'clock, legend as a single column, medium text size
			legend(size(medium) position(12) bm(t=1 b=0 l=0 r=0) colf cols(1)
			/// Legend color is white, order text with 3rd command first (so Caribbean first)
			region(fcolor(gs16) lw(vthin) margin(l=2 r=2 t=2 b=2)) order(3 1 2)
			lab(1 "Central America")
			lab(2 "Southern America")
			lab(3 "Caribbean")
			)
			name(graph5)
			;
#delimit cr


** 06 - Format GRAPH  region (colour of graph background, overall graph shape)
#delimit ;
	gr twoway 
		  (connect value year if measure==7 & type==16)
		  (connect value year if measure==7 & type==17)
		  (connect value year if measure==7 & type==15)
		  ,
			/// Plotregion colors always white (gs16)
			plotregion(c(gs16) ic(gs16) ilw(thin) lw(thin)) 		
			/// Graphregion colors always white (gs16)
			graphregion(color(gs16) ic(gs16) ilw(thin) lw(thin)) 
			/// Shape of graph Y-axis = 7 (larger values for Y make y-axis longer relative to x-axis
			ysize(7)

			xlab(1965 "1965-70" 1975 "1975-80" 1985 "1985-90" 1995 "1995-00" 2005 "2005-10", labs(medium) nogrid angle(45))
			xtitle("Year", margin(t=3) size(medlarge)) 
			xmtick(1965(5)2010)

			ylab(0(20)80, labs(medium) nogrid glc(gs14) angle(0) format(%9.0f))
			ytitle("STI disparity measure", margin(r=3) size(medlarge))
			ytick(0(10)80) ymtick(0(5)80)

			legend(size(medium) position(12) bm(t=1 b=0 l=0 r=0) colf cols(1)
			region(fcolor(gs16) lw(vthin) margin(l=2 r=2 t=2 b=2)) order(3 1 2)
			lab(1 "Central America")
			lab(2 "Southern America")
			lab(3 "Caribbean")
			)			
			name(graph6)
			;
#delimit cr


** 07 - Format the graph LINES
#delimit ;
	gr twoway 
		  /// lw=line width, msize=symbol size, mc=symbol colour, lc=line color
		  /// Colours use RGB system
		  (connect value year if measure==7 & type==16, lp("-") lw(medium) msize(medium) mc("116 196 118") lc("116 196 118"))
		  (connect value year if measure==7 & type==17, lp("-") lw(medium) msize(medium) mc("186 228 179") lc("186 228 179"))
		  (connect value year if measure==7 & type==15, lp("l") lw(medium) msize(medium) mc("0 109 44") lc("0 109 44"))
		  ,
			plotregion(c(gs16) ic(gs16) ilw(thin) lw(thin)) 		
			graphregion(color(gs16) ic(gs16) ilw(thin) lw(thin)) 
			ysize(7)

			xlab(1965 "1965-70" 1975 "1975-80" 1985 "1985-90" 1995 "1995-00" 2005 "2005-10", labs(medium) nogrid angle(45))
			xtitle("Year", margin(t=3) size(medlarge)) 
			xmtick(1965(5)2010)

			ylab(0(20)80, labs(medium) nogrid glc(gs14) angle(0) format(%9.0f))
			ytitle("STI disparity measure", margin(r=3) size(medlarge))
			ytick(0(10)80) ymtick(0(5)80)

			legend(size(medium) position(12) bm(t=1 b=0 l=0 r=0) colf cols(1)
			region(fcolor(gs16) lw(vthin) margin(l=2 r=2 t=2 b=2)) order(3 1 2)
			lab(1 "Central America")
			lab(2 "Southern America")
			lab(3 "Caribbean")
			)			
			name(graph7)
			;
#delimit cr

